﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Budget
{
    public partial class YearCategory : Form
    {
        decimal total = 0;
        DataTable dt_expenses;
        public YearCategory()
        {
            InitializeComponent();
            foreach (string s in dbData.listExpenseCategories)
            {
                ddlCategory.Items.Add(s);
            }
            ddlCategory.SelectedIndex = 0;
            AddExpensesToListView();
        }
        private void AddExpensesToListView()
        {
            total = 0;
            lv_cat_tot.Clear();
            lv_cat_tot.FullRowSelect = true;
            lv_cat_tot.View = View.Details;
            int fridays = Dates.getWeeksInMonth(dateTimePicker1.Value.Month, dateTimePicker1.Value.Year);
            List<ListViewGroup> MonthGroup = new List<ListViewGroup>();
            lv_cat_tot.Columns.Add("Amount", 200, HorizontalAlignment.Left);

            DateTime temp = dateTimePicker1.Value;
            DateTime start_date;
            DateTime end_date;

            if(cbLastTwelve.Checked)
            {
                start_date = Dates.getFirstTransactionDayOfMonth(new DateTime(temp.Year, temp.Month, temp.Day).AddYears(-1).AddMonths(1));
                end_date = Dates.getLastTransactionDayofMonth(temp);
            }
            else
            {
                start_date = Dates.getFirstTransactionDayOfMonth(new DateTime(dateTimePicker1.Value.Year, 1, 1));
                end_date = Dates.getLastTransactionDayofMonth(new DateTime(dateTimePicker1.Value.Year, 12, 1));
            }

            for (int i = 0; i < 12; i++ )
            {
                MonthGroup.Add(new ListViewGroup(start_date.AddDays(10).ToString("MMMM  yyyy") , System.Windows.Forms.HorizontalAlignment.Left));
                lv_cat_tot.Groups.Add(MonthGroup[MonthGroup.Count - 1]);
                start_date = start_date.AddMonths(1);
            }

            List<ListViewItem> ExpenseList = new List<ListViewItem>();
            ListViewItem item;

            

            temp = dateTimePicker1.Value;
            if(cbLastTwelve.Checked)
            {
                start_date = Dates.getFirstTransactionDayOfMonth(new DateTime(temp.Year, temp.Month, temp.Day).AddYears(-1).AddMonths(1));
                end_date = Dates.getLastTransactionDayofMonth(temp);
            }
            else
            {
                start_date = Dates.getFirstTransactionDayOfMonth(new DateTime(dateTimePicker1.Value.Year, 1, 1));
                end_date = Dates.getLastTransactionDayofMonth(new DateTime(dateTimePicker1.Value.Year, 12, 1));
            }

            dt_expenses = DatabaseQuery.ExecStringProc(
                        StoredProcs.getYearToDateByCategoryForMonth(
                            start_date.ToString("MM/dd/yyyy"),
                            end_date.ToString("MM/dd/yyyy"),
                            ddlCategory.SelectedItem.ToString()
                                                     )
                );

            //loop through each row
            for (int i = 0; i < 12 ; i++)
            {
                decimal monthTotal = 0;
                while(true)
                {
                    if(dt_expenses.Rows.Count == 0 || 
                        Convert.ToDateTime(dt_expenses.Rows[0]["Date"]) > Dates.getLastTransactionDayofMonth(start_date.AddMonths(i).AddDays(10))
                        )
                    {
                        break;
                    }
                    
                    monthTotal += Convert.ToDecimal(dt_expenses.Rows[0]["Amount"]);
                    total += Convert.ToDecimal(dt_expenses.Rows[0]["Amount"]);
                    dt_expenses.Rows.RemoveAt(0);
                }

                //create a new row
                item = new ListViewItem(monthTotal.ToString());
                
                //set the group equal to the Month
                item.Group = MonthGroup[i];
                
                //store in list, we will add after we are done
                ExpenseList.Add(item);
                monthTotal = 0;
            }
            //populate the listview from out list
            foreach (ListViewItem lvi in ExpenseList)
            {
                lv_cat_tot.Items.Add(lvi);
            }
            lblTotalVal.Text = total.ToString();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }

        private void YearCategory_SizeChanged(object sender, EventArgs e)
        {
            lv_cat_tot.Height = YearCategory.ActiveForm.Height - 104;
            lblTotalVal.Location = new Point(lblTotalVal.Location.X, YearCategory.ActiveForm.Height - 63);
            label1.Location = new Point(label1.Location.X, YearCategory.ActiveForm.Height - 63);
        }

        private void cbLastTwelve_CheckedChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            AddExpensesToListView();
        }
    }
}
